/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import componenten.Database;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
 * @author daveronvanderham
 */
public class Masterclass {

    private int masterclass_id;
    private int master;
    private String datum;
    private String tijd;
    private String min_rating;
    private int min_deelnemers;
    private double inleggeld;
    private String locatie_postcode;
    private String locatie_huisnummer;
    private String status;

    public Masterclass(int Master, String Datum, String Tijd, String Min_rating, int Min_deelnemers, double Inleggeld, String Postcode, String Huisnr,String Status ) {
        this.master = Master;
        this.datum = Datum;
        this.tijd = Tijd;
        this.min_rating = Min_rating;
        this.min_deelnemers = Min_deelnemers;
        this.inleggeld = Inleggeld;
        this.locatie_postcode = Postcode;
        this.locatie_huisnummer = Huisnr;
        this.status = Status;
    }

    public Masterclass(int Masterclass_id) {
        String sql = "SELECT * FROM masterclass WHERE masterclass_id = '" + Masterclass_id + "'";
        System.out.print(sql);
        try {
            ResultSet result = Database.executeSelectQuery(sql);
            result.next();
            this.masterclass_id = Masterclass_id;
            this.datum = result.getString("masterclass_datum");
            this.master = result.getInt("master_id");
            this.tijd = result.getString("masterclass_tijd");
            this.inleggeld = result.getDouble("masterclass_inleggeld");
            this.min_rating = result.getString("masterclass_min_rating");
            this.min_deelnemers = result.getInt("masterclass_min_deelnemers");
            this.locatie_postcode = result.getString("locatie_postcode");
            this.locatie_huisnummer = result.getString("locatie_huisnummer");
            this.status = result.getString("status");

        } catch (SQLException e) {
            System.out.print(e);
        }
    }

    /**
     * @return the master
     */
    public int getMaster() {
        return master;
    }

    /**
     * @param master the master to set
     */
    public void setMaster(int master) {
        try {
            String query = "SELECT persoon_id FROM master where master_bijnaam = " + master + "";
            ResultSet rs = Database.executeSelectQuery(query);
            rs.next();
            master = rs.getInt("persoon_id");
            this.master = master;
        } catch (Exception e) {
        }
    }

    public String gerMasterBijnaam(int master){
        String  master_bijnaam = "";
        try {
            String query = "SELECT master_bijnaam FROM master where persoon_id = " + master + "";
            ResultSet rs = Database.executeSelectQuery(query);
            rs.next();
            master_bijnaam = rs.getString("master_bijnaam");
            } catch (Exception e) {
        }
        return master_bijnaam;
    }
    /**
     * @return the datum
     */
    public String getDatum() {
        return datum;
    }

    /**
     * @param datum the datum to set
     */
    public void setDatum(String datum) {
        this.datum = datum;

    }

    /**
     * @return the tijd
     */
    public String getTijd() {

        return tijd;
    }

    /**
     * @param tijd the tijd to set
     */
    public void setTijd(String tijd) {

        this.tijd = tijd;
    }

    /**
     * @return the min_rating
     */
    public String getMin_rating() {
        return min_rating;
    }

    /**
     * @param min_rating the min_rating to set
     */
    public void setMin_rating(String min_rating) {
        this.min_rating = min_rating;
    }

    /**
     * @return the min_deelnemers
     */
    public int getMin_deelnemers() {
        return min_deelnemers;
    }

    /**
     * @param min_deelnemers the min_deelnemers to set
     */
    public void setMin_deelnemers(int min_deelnemers) {
        this.min_deelnemers = min_deelnemers;
    }

    /**
     * @return the inleggeld
     */
    public double getInleggeld() {
        return inleggeld;
    }

    /**
     * @param inleggeld the inleggeld to set
     */
    public void setInleggeld(double inleggeld) {
        this.inleggeld = inleggeld;
    }

    /**
     * @return the lokatie_postcode
     */
    public String getLocatie_postcode() {
        return locatie_postcode;
    }

    /**
     * @param lokatie_postcode the lokatie_postcode to set
     */
    public void setLocatie_postcode(String lokatie_postcode) {
        this.locatie_postcode = lokatie_postcode;
    }

    /**
     * @return the locatie_huisnummer
     */
    public String getLocatie_huisnummer() {
        return locatie_huisnummer;
    }

    /**
     * @param locatie_huisnummer the locatie_huisnummer to set
     */
    public void setLocatie_huisnummer(String locatie_huisnummer) {
        this.locatie_huisnummer = locatie_huisnummer;
    }

    public boolean insert() {
        boolean successful = false;

        //sla persoon op in de database, mbv  Database.executeInsert
        //de code sla je niet op, die wordt automatisch gegenereerd
        String query = "Insert into masterclass( masterclass_datum, masterclass_tijd, masterclass_min_rating, masterclass_min_deelnemers, masterclass_inleggeld, master_id, locatie_postcode, locatie_huisnummer, status) values('"
                + getDatum() + "','"
                + getTijd() + "',"
                + getMin_rating() + ","
                + getMin_deelnemers() + ","
                + getInleggeld() + ","
                + getMaster() + ",'"
                + getLocatie_postcode() + "',"
                + getLocatie_huisnummer() + "','"
                + getStatus() +   "')";

        //verkorte if ... else constructie met ? en :
        if (Database.executeInsertQuery(query) == 1) { //tenminste 1 record inserted
            JOptionPane.showMessageDialog(null, "masterclass toegevoegd");
            successful = true;
        }

        return successful;
    }

    public boolean update() {

        boolean successful = false;

        //sla persoon op in de database, mbv  Database.executeInsert
        //de code sla je niet op, die wordt automatisch gegenereerd
        String query = "UPDATE masterclass SET"      
                + " status ='" + getStatus() + "' WHERE masterclass_id = " + getMasterclass_id() + "";

        //verkorte if ... else constructie met ? en :
        if (Database.executeUpdateQuery(query) == 1) { //tenminste 1 record inserted
            successful = true;
        }

        return successful;
    }

    /**
     * @return the masterclass_id
     */
    public int getMasterclass_id() {
        return masterclass_id;
    }

    /**
     * @param masterclass_id the masterclass_id to set
     */
    public void setMasterclass_id(int masterclass_id) {
        this.masterclass_id = masterclass_id;
    }

    /**
     * @return the status
     */
    public String getStatus() {
        return status;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(String status) {
        this.status = status;
    }
}